import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;


public class DisciplineModel {
	
	/**
	 * Creates a new discipline
	 * @param code        Code of the discipline
	 * @param description Description of the discipline
	 * @param punish      Punishment of the discipline
	 * @return True on success, false on failure
	 */
	public static boolean addDiscipline( int code, String description, String punish){
		return DB.SafeUpdate( "INSERT INTO `Discipline` ( `code`, `description`, `punishment` ) VALUES ( ?, ?, ? )", new String[]{"" + code, description, punish} );
//		return DB.Update( "INSERT INTO `Discipline` ( `code`, `description`, `punishment` ) VALUES ( " + code + ", '" + description + "', '" + punish + "')" );
	}
	
	/**
	 * Gets a full list of all disciplines
	 * @return Array of all disciplines, null on error
	 */
	public static Discipline[] getDisciplines(){
		ResultSet rs = DB.Query( "SELECT * FROM Discipline" );
		Discipline[] d = null;
		
		if( rs != null ){
			try{
				// Init array
				rs.last();
				d = new Discipline[rs.getRow()];
				rs.first();
				
				// Fill array
				for( int i = 0; i < d.length; ++i, rs.next() ){
					Discipline tmp = new Discipline();
					tmp.code = rs.getInt( "code" );
					tmp.description = rs.getString( "description" );
					tmp.punishment = rs.getString( "punishment" );
					d[i] = tmp;
				}
				
			}catch( SQLException e ){
				// SQL Error, return null
				System.err.println( e.getMessage() );
				d = null;
			}
		}
		
		return d;
	}
	
	/**
	 * Gets disciplines assigned to a club
	 * @param c Club
	 * @return  Array of disciplines, or null on error
	 */
	public static ClubDiscipline[] getDisciplines( Club c ){
		return getDisciplines( c.id );
	}
	
	/**
	 * Gets disciplines assigned to a club
	 * @param c Club id
	 * @return  Array of disciplines, or null on error
	 */
	public static ClubDiscipline[] getDisciplines( int club_id ){
		ResultSet rs = DB.Query( "SELECT * FROM Discipline NATURAL JOIN Receives_Discipline WHERE club_id=" + club_id );
		ClubDiscipline[] d = null;
		
		if( rs != null ){
			try{
				// Init array
				rs.last();
				d = new ClubDiscipline[rs.getRow()];
				rs.first();
				
				// Fill array
				for( int i = 0; i < d.length; ++i, rs.next() ){
					ClubDiscipline tmp = new ClubDiscipline();
					tmp.code = rs.getInt( "code" );
					tmp.description = rs.getString( "description" );
					tmp.punishment = rs.getString( "punishment" );
					tmp.reason = rs.getString( "reason" );
					tmp.expires = rs.getDate( "expiration" );
					d[i] = tmp;
				}
				
			}catch( SQLException e ){
				// SQL Error, return null
				System.err.println( e.getMessage() );
				d = null;
			}
		}
		
		return d;
	}
	
	/**
	 * Assigns a discipline to a club
	 * @param dis_code Code of the discipline
	 * @param club_id  Club
	 * @return True on success, false on failure
	 */
	public static boolean disciplineClub( int dis_code, Club club, String reason, Date expire ){
		return disciplineClub( dis_code, club.id, reason, expire );
	}
	
	/**
	 * Assigns a discipline to a club
	 * @param dis_code Discipline
	 * @param club_id  Club
	 * @return True on success, false on failure
	 */
	public static boolean disciplineClub( Discipline dis, Club club, String reason, Date expire ){
		return disciplineClub( dis.code, club.id, reason, expire );
	}
	
	/**
	 * Assigns a discipline to a club
	 * @param dis_code Discipline
	 * @param club_id  Id of the club
	 * @return True on success, false on failure
	 */
	public static boolean disciplineClub( Discipline dis, int club_id, String reason, Date expire ){
		return disciplineClub( dis.code, club_id, reason, expire );
	}
	
	/**
	 * Assigns a discipline to a club
	 * @param dis_code Code of the discipline
	 * @param club_id  Id of the club
	 * @return True on success, false on failure
	 */
	public static boolean disciplineClub( int dis_code, int club_id, String reason, Date expire ){
		return DB.Update( "INSERT INTO Receives_Discipline ( `club_id`, `code`, `reason`, `expiration` ) VALUES ( " + club_id +
				", " + dis_code + ", '" + reason + "', '" + expire.toString() + "' )" );
	}
}
